package com.dhcc.core.modules.msgcenter;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.framework.util.SpringContextHolder;
import com.dhcc.core.framework.util.properties.GlobalProperties;
import com.dhcc.core.modules.msgcenter.cache.MsgCache;
import com.dhcc.core.modules.msgcenter.constant.MsgReceiveType;
import com.dhcc.core.modules.msgcenter.entity.MsgModule;
import com.dhcc.core.modules.msgcenter.push.AbsPushService;
import com.dhcc.core.modules.msgcenter.service.IMsgPushLogService;
import com.dhcc.core.modules.msgcenter.vo.MsgPush;
import com.dhcc.core.modules.system.entity.Dept;
import com.dhcc.core.modules.system.entity.Group;
import com.dhcc.core.modules.system.entity.Position;
import com.dhcc.core.modules.system.entity.Role;
import com.dhcc.core.modules.system.entity.User;

/**
 * 消息推送工具-异步
 * @ClassName: MsgPushUtil 
 * @Description: TODO
 * @author: cyf
 * @date: 2018年11月20日 上午9:58:02
 */
@Component
@Async
public class MsgPushUtil {
    @Autowired
    private IMsgPushLogService msgPushLogService;
    public static MsgPushUtil me() {
        return SpringContextHolder.getBean(MsgPushUtil.class);
    }
    public void push(MsgPush mp){
        GlobalProperties gp = GlobalProperties.me();
        String types = gp.getProperty("sys.msg.type");
        // 之前发失败的重发
        if(CommonUtil.isNotEmpty(mp.getLogId())){
            String pt = mp.getPushType();
            if(CommonUtil.contains(types,pt)){
                String beanName = gp.getProperty("sys.msg."+pt+".beanName");
                AbsPushService bean = SpringContextHolder.getBean(beanName);
                bean.send(mp,pt);
                if(CommonUtil.isNotEmpty(mp.getLogId())){
                    msgPushLogService.deleteById(mp.getLogId());
                }
            }
        }else{
            // 新消息的发送
            MsgModule module = mp.getModule();
            List<String> pushTypeList = module.getPushTypeList();
            if(CommonUtil.isNotEmpty(pushTypeList)){
                for (String pt : pushTypeList) {
                    if(CommonUtil.contains(types,pt)){
                        String beanName = gp.getProperty("sys.msg."+pt+".beanName");
                        AbsPushService bean = SpringContextHolder.getBean(beanName);
                        bean.send(mp,pt);
                    }
                }
            }
        }
    }
    public void pushAll(String moduleCode,String title, String content, Map<String,Object> datas,User sendUser){
        MsgPush mp = new MsgPush();
        MsgModule module = MsgCache.me().getMsgModuleByCode(moduleCode);
        mp.setModule(module);
        mp.setMsgTitle(title);
        mp.setMsgContent(content);
        mp.setDataMap(datas);
        mp.setReceiveType(MsgReceiveType.ALL.getCode());
        mp.setSendUser(sendUser);
        push(mp);
    }
    public void pushUser(String moduleCode,String title, String content, Map<String,Object> datas,User sendUser,String receiveIds,String receiveNames){
        MsgPush mp = new MsgPush();
        MsgModule module = MsgCache.me().getMsgModuleByCode(moduleCode);
        mp.setModule(module);
        mp.setMsgTitle(title);
        mp.setMsgContent(content);
        mp.setDataMap(datas);
        mp.setReceiveType(MsgReceiveType.USER.getCode());
        mp.setReceiveIds(receiveIds);
        mp.setReceiveNames(receiveNames);
        mp.setSendUser(sendUser);
        push(mp);
    }
    public void pushDept(String moduleCode,String title, String content, Map<String,Object> datas,User sendUser,String receiveIds,String receiveNames){
        MsgPush mp = new MsgPush();
        MsgModule module = MsgCache.me().getMsgModuleByCode(moduleCode);
        mp.setModule(module);
        mp.setMsgTitle(title);
        mp.setMsgContent(content);
        mp.setDataMap(datas);
        mp.setReceiveType(MsgReceiveType.DEPT.getCode());
        mp.setReceiveIds(receiveIds);
        mp.setReceiveNames(receiveNames);
        mp.setSendUser(sendUser);
        push(mp);
    }
    public void pushGroup(String moduleCode,String title, String content, Map<String,Object> datas,User sendUser,String receiveIds,String receiveNames){
        MsgPush mp = new MsgPush();
        MsgModule module = MsgCache.me().getMsgModuleByCode(moduleCode);
        mp.setModule(module);
        mp.setMsgTitle(title);
        mp.setMsgContent(content);
        mp.setDataMap(datas);
        mp.setReceiveType(MsgReceiveType.GROUP.getCode());
        mp.setReceiveIds(receiveIds);
        mp.setReceiveNames(receiveNames);
        mp.setSendUser(sendUser);
        push(mp);
    }
    public void pushRole(String moduleCode,String title, String content, Map<String,Object> datas,User sendUser,String receiveIds,String receiveNames){
        MsgPush mp = new MsgPush();
        MsgModule module = MsgCache.me().getMsgModuleByCode(moduleCode);
        mp.setModule(module);
        mp.setMsgTitle(title);
        mp.setMsgContent(content);
        mp.setDataMap(datas);
        mp.setReceiveType(MsgReceiveType.ROLE.getCode());
        mp.setReceiveIds(receiveIds);
        mp.setReceiveNames(receiveNames);
        mp.setSendUser(sendUser);
        push(mp);
    }
    public void pushPosition(String moduleCode,String title, String content, Map<String,Object> datas,User sendUser,String receiveIds,String receiveNames){
        MsgPush mp = new MsgPush();
        MsgModule module = MsgCache.me().getMsgModuleByCode(moduleCode);
        mp.setModule(module);
        mp.setMsgTitle(title);
        mp.setMsgContent(content);
        mp.setDataMap(datas);
        mp.setReceiveType(MsgReceiveType.POSITION.getCode());
        mp.setReceiveIds(receiveIds);
        mp.setReceiveNames(receiveNames);
        mp.setSendUser(sendUser);
        push(mp);
    }

    public void pushUser(String moduleCode,String title, String content, Map<String,Object> datas,User sendUser,List<User> receiveUsers){
        MsgPush mp = new MsgPush();
        MsgModule module = MsgCache.me().getMsgModuleByCode(moduleCode);
        mp.setModule(module);
        mp.setMsgTitle(title);
        mp.setMsgContent(content);
        mp.setDataMap(datas);
        mp.setReceiveType(MsgReceiveType.USER.getCode());
        mp.setReceiveUsers(receiveUsers);
        mp.setSendUser(sendUser);
        push(mp);
    }
    public void pushDept(String moduleCode,String title, String content, Map<String,Object> datas,User sendUser,List<Dept> receiveDepts){
        MsgPush mp = new MsgPush();
        MsgModule module = MsgCache.me().getMsgModuleByCode(moduleCode);
        mp.setModule(module);
        mp.setMsgTitle(title);
        mp.setMsgContent(content);
        mp.setDataMap(datas);
        mp.setReceiveType(MsgReceiveType.DEPT.getCode());
        mp.setReceiveDepts(receiveDepts);
        mp.setSendUser(sendUser);
        push(mp);
    }
    public void pushGroup(String moduleCode,String title, String content, Map<String,Object> datas,User sendUser,List<Group> receiveGroups){
        MsgPush mp = new MsgPush();
        MsgModule module = MsgCache.me().getMsgModuleByCode(moduleCode);
        mp.setModule(module);
        mp.setMsgTitle(title);
        mp.setMsgContent(content);
        mp.setDataMap(datas);
        mp.setReceiveType(MsgReceiveType.GROUP.getCode());
        mp.setReceiveGroups(receiveGroups);
        mp.setSendUser(sendUser);
        push(mp);
    }
    public void pushRole(String moduleCode,String title, String content, Map<String,Object> datas,User sendUser,List<Role> receiveRoles){
        MsgPush mp = new MsgPush();
        MsgModule module = MsgCache.me().getMsgModuleByCode(moduleCode);
        mp.setModule(module);
        mp.setMsgTitle(title);
        mp.setMsgContent(content);
        mp.setDataMap(datas);
        mp.setReceiveType(MsgReceiveType.ROLE.getCode());
        mp.setReceiveRoles(receiveRoles);
        mp.setSendUser(sendUser);
        push(mp);
    }
    public void pushPosition(String moduleCode,String title, String content, Map<String,Object> datas,User sendUser,List<Position> receivePositions){
        MsgPush mp = new MsgPush();
        MsgModule module = MsgCache.me().getMsgModuleByCode(moduleCode);
        mp.setModule(module);
        mp.setMsgTitle(title);
        mp.setMsgContent(content);
        mp.setDataMap(datas);
        mp.setReceiveType(MsgReceiveType.POSITION.getCode());
        mp.setReceivePositions(receivePositions);
        mp.setSendUser(sendUser);
        push(mp);
    }
}

